package cn.sinonet.sms.dao.jpa;

import java.util.List;

import javax.persistence.Query;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.sinonet.sms.dao.DQZFDao;
import cn.sinonet.sms.model.DQZF;

@Repository("dqzfDao")
public class DQZFDaoJpa extends GenericDaoJpa<DQZF, Long> implements DQZFDao {

	@Autowired
	private DataSource dataSource;

	/**
	 * Constructor that sets the entity to User.class.
	 */
	public DQZFDaoJpa() {
		super(DQZF.class);
	}

	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public DQZF loadByMessageAndPhone(String message, String phone, String time) {
		// TODO Auto-generated method stub

		Query q = getEntityManager()
				.createQuery(
						"select t from DQZF as t where message=? and phoneNumber=? and createdTime=?");
		q.setParameter(1, message);
		q.setParameter(2, phone);
		q.setParameter(3, time);
		List<DQZF> bean = q.getResultList();
		if (bean == null || bean.isEmpty()) {
			return null;
		} else {
			return bean.get(0);
		}

	}
	
	
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public DQZF loadByMessageAndPhone(String message, String phone) {
		// TODO Auto-generated method stub

		Query q = getEntityManager()
				.createQuery(
						"select t from DQZF as t where message=? and phoneNumber=?");
		q.setParameter(1, message);
		q.setParameter(2, phone);
		List<DQZF> bean = q.getResultList();
		if (bean == null || bean.isEmpty()) {
			return null;
		} else {
			return bean.get(0);
		}

	}

}
